package com.qys.livesMall.shop.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qys.livesMall.app.user.model.vo.app.AppUserInfoTeamDetailVo;
import com.qys.livesMall.common.constants.Constants;
import com.qys.livesMall.shop.constants.ShopConstant;
import com.qys.livesMall.shop.dao.H2ShopBaseDao;
import com.qys.livesMall.shop.mapper.H2ShopBaseMapper;
import com.qys.livesMall.shop.modal.dto.shop.ShopQuery;
import com.qys.livesMall.shop.modal.dto.shop.shopBaseSettlementDay;
import com.qys.livesMall.shop.modal.entity.H2ShopBase;
import com.qys.livesMall.shop.modal.vo.ShopVO;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @menu H2ShopBaseDaoImpl
 * @date 2023/05/26 11:08
 */
@Service
public class H2ShopBaseDaoImpl extends ServiceImpl<H2ShopBaseMapper, H2ShopBase> implements H2ShopBaseDao {

    @Override
    public H2ShopBase getByUserId(Long userId) {
        LambdaQueryWrapper<H2ShopBase> qw=new LambdaQueryWrapper<>();
        qw.eq(H2ShopBase::getUserId,userId);
        qw.last("limit 1");
        return getOne(qw);
    }

    @Override
    public Long queryShopBaseIdByUserId(Long userId) {
        return baseMapper.queryShopBaseIdByUserId(userId);
    }

    @Override
    public long getUserInviteShopCount(List<Long> userIds) {
        LambdaQueryWrapper<H2ShopBase> qw=new LambdaQueryWrapper<>();
        qw.in(H2ShopBase::getUserId,userIds);
        qw.eq(H2ShopBase::getStatus, ShopConstant.ShopStatus.IN_BUSINESS);
        return count(qw);
    }

    @Override
    public H2ShopBase getByShopCode(Integer shopCode) {
        LambdaQueryWrapper<H2ShopBase> qw=new LambdaQueryWrapper<>();
        qw.eq(H2ShopBase::getShopCode,shopCode);
        qw.eq(H2ShopBase::getStatus, ShopConstant.ShopStatus.IN_BUSINESS);
        qw.last("limit 1");
        return getOne(qw);
    }

    @Override
    public List<H2ShopBase> getByShopName(String shopName) {
        LambdaQueryWrapper<H2ShopBase> qw=new LambdaQueryWrapper<>();
        qw.like(H2ShopBase::getShopName,shopName);
        return list(qw);
    }

    @Override
    public List<H2ShopBase> getByShopName(String shopName, String status,List<Long> userIds) {
        LambdaQueryWrapper<H2ShopBase> qw=new LambdaQueryWrapper<>();
        qw.like(StringUtils.isNotBlank(shopName),H2ShopBase::getShopName,shopName);
        if (status!=null) {
            qw.eq(H2ShopBase::getStatus, status);
        }
        if (CollectionUtils.isNotEmpty(userIds)){
            qw.in(H2ShopBase::getUserId,userIds);
        }
        return list(qw);
    }

    @Override
    public boolean existByLevel(Integer levelId) {
        LambdaQueryWrapper<H2ShopBase> qw=new LambdaQueryWrapper<>();
        qw.eq(H2ShopBase::getShopLevelId,levelId);
        qw.last("limit 1");
        return getOne(qw)!=null?true:false;
    }

    @Override
    public List<Long> existByUserIds(List<Long> userIds) {
        LambdaQueryWrapper<H2ShopBase> qw=new LambdaQueryWrapper<>();
        qw.select(H2ShopBase::getUserId);
        qw.in(H2ShopBase::getUserId,userIds);
        List<H2ShopBase> shops=list(qw);
        return shops==null?null:shops.stream().map(H2ShopBase::getUserId).collect(Collectors.toList());
    }

    @Override
    public List<H2ShopBase> getShopByUserIds(List<Long> userIds) {
        LambdaQueryWrapper<H2ShopBase> qw=new LambdaQueryWrapper<>();
        qw.select(H2ShopBase::getId,H2ShopBase::getStatus,H2ShopBase::getUserId,H2ShopBase::getType,H2ShopBase::getRecharged,H2ShopBase::getCreateTime,H2ShopBase::getShopLevelId);
        qw.in(H2ShopBase::getUserId,userIds);
        List<H2ShopBase> shops=list(qw);
        return shops;
    }

    @Override
    public boolean setShopSettlementDay(shopBaseSettlementDay shopBaseSettlementDay) {
        LambdaUpdateWrapper<H2ShopBase> qw=new LambdaUpdateWrapper<>();
        qw.set(H2ShopBase::getSettlementDay,shopBaseSettlementDay.getSettlementDay());
        qw.in(H2ShopBase::getId,shopBaseSettlementDay.getShopIds());
        update(qw);
        return false;
    }

    @Override
    public long getActiveShopCount(List<Long> userIds) {
        LambdaQueryWrapper<H2ShopBase> qw=new LambdaQueryWrapper<>();
        qw.eq(H2ShopBase::getStatus,ShopConstant.ShopStatus.IN_BUSINESS);
        qw.in(H2ShopBase::getUserId,userIds);
        qw.eq(H2ShopBase::getEarnestMoney, Constants.YesNoInt.YES);
        return count(qw);
    }

    @Override
    public List<Long> getShopIdByQuery(ShopQuery query) {
        boolean exc=false;
        LambdaQueryWrapper<H2ShopBase> qw=new LambdaQueryWrapper<>();
        qw.select(H2ShopBase::getId);
        if (query.getUserIds()!=null){
            if (query.getUserIds().size()==0){
                query.getUserIds().add(-1l);
            }
            qw.in(H2ShopBase::getUserId,query.getUserIds());
            exc=true;
        }
        if (query.getShopStatus()!=null) {
            qw.eq(H2ShopBase::getStatus, query.getShopStatus());
            exc=true;
        }
        if (query.getShopCode()!=null){
            qw.eq(H2ShopBase::getShopCode,query.getShopCode());
            exc=true;
        }
        if (query.getShopLevelId()!=null){
            qw.eq(H2ShopBase::getShopLevelId,query.getShopLevelId());
            exc=true;
        }
        if (query.getApplyIp()!=null){
            qw.eq(H2ShopBase::getApplyIp,query.getApplyIp());
            exc=true;
        }
        if (org.apache.commons.lang3.StringUtils.isNotBlank(query.getShopName())){
            qw.like(H2ShopBase::getShopName,query.getShopName());
            exc=true;
        }
        if (org.apache.commons.lang3.StringUtils.isNotBlank(query.getPhone())){
            qw.like(H2ShopBase::getPhone,query.getPhone());
            exc=true;
        }
        if (query.getStartTime()!=null){
            qw.ge(H2ShopBase::getCreateTime,query.getStartTime());
            exc=true;
        }
        if (query.getEndTime()!=null){
            qw.le(H2ShopBase::getCreateTime,query.getEndTime());
            exc=true;
        }
        if (!exc){
            return null;
        }
        List<H2ShopBase> list=list(qw);
        if (CollectionUtils.isNotEmpty(list)){
            return list.stream().map(H2ShopBase::getId).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

    @Override
    public List<H2ShopBase> getByPage(Integer pageNum, Integer size) {
        LambdaQueryWrapper<H2ShopBase> qw=new LambdaQueryWrapper<>();
        qw.select(H2ShopBase::getId,H2ShopBase::getUserId );
        qw.last("limit "+(pageNum-1)*size+","+size);
        List<H2ShopBase> shops=list(qw);
        return shops;
    }

    @Override
    public List<AppUserInfoTeamDetailVo> getShopbyids(Page<AppUserInfoTeamDetailVo> page, List<Long> shopids) {
        return baseMapper.getShopbyids(page,shopids);
    }

    @Override
    public List<H2ShopBase> getYesterday() {
        return baseMapper.getYesterday();
    }

    @Override
    public List<H2ShopBase> getAllShop() {
        LambdaQueryWrapper<H2ShopBase> qw=new LambdaQueryWrapper<>();
        qw.select(H2ShopBase::getId,H2ShopBase::getUserId,H2ShopBase::getSettlementDay,H2ShopBase::getShopLevelId );
        qw.eq(H2ShopBase::getStatus,ShopConstant.ShopStatus.IN_BUSINESS);
        List<H2ShopBase> shops=list(qw);
        return shops;
    }

}

